package com.yanqu.road.server.cmd.activity.carptrip;

import com.yanqu.road.dao.impl.activity.carptrip.CarpTripPvPReportDataDaoImpl;
import com.yanqu.road.entity.activity.carptrip.data.CarpTripPvPReportData;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.CarpTripProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.carptrip.CarpTripModule;
import com.yanqu.road.server.manger.activity.carptrip.CarpTripMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = Protocol.S_CARP_TRIP_PVP_FIGHT_REPORT_FROM_CROSS, desc = "PVP跨服返回战报")
public class CarpTripPVPReportFromCrossCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {

        CarpTripProto.CarpTripPVPReportTemp reportTemp = CarpTripProto.CarpTripPVPReportTemp.parseFrom(yanQuMessage.getBytes());

        CarpTripPvPReportData data = initReport(reportTemp, yanQuMessage.getPlayerId(), CarpTripMgr.getActivityId());

        GamePlayer player = GamePlayerMgr.getOnlinePlayer(yanQuMessage.getPlayerId());
        if(player != null){
            player.getModule(CarpTripModule.class).addReport(data);
        }else {
            new CarpTripPvPReportDataDaoImpl().add(data);
        }
    }


    public CarpTripPvPReportData initReport(CarpTripProto.CarpTripPVPReportTemp reportTemp, long userId, int activityId){
        CarpTripPvPReportData reportData = new CarpTripPvPReportData();
        reportData.setActivityId(activityId);
        reportData.setUserId(userId);
        reportData.setEnemyUserId(reportTemp.getEnemyId());
        reportData.setAbility(reportTemp.getMyAbility());
        reportData.setEnemyUserBaseInfo(PlayerBasePb.parseToUserBaseInfo(reportTemp.getBaseInfo()));
        reportData.setCellId(reportTemp.getOldCellId());
        reportData.setOldCellNum(reportTemp.getOldCellId());
        reportData.setNewCellNum(reportTemp.getNewCellId());
        reportData.setEnemyAbility(reportTemp.getAbility());
        reportData.setTime(System.currentTimeMillis());
        reportData.setInsertOption();
        return reportData;
    }
}
